<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:59 AM
 */

namespace app\index\service;
use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\common\service\DownService;
use app\common\service\SkuService;
use think\Db;
use think\Exception;

class ShopwarehousecostService
{
    static public function getList($param, $is_download = false)
    {
        try{
            $where = "where 1 = 1 ";
            $pageNumber = isset($param['page']) ? $param['page'] : 1;
            $pageData = isset($param['limit']) ? $param['limit'] : 20;
            $warehouse_shop_permission = ConfigBase::getWarehouseShopPermission();  // 仓库权限
            $id = isset($param['id']) ? $param['id'] : '';  // ID
            $base_warehouse_id = isset($param['base_warehouse_id']) ? $param['base_warehouse_id'] : '';  // 收货仓库ID
            $batch = isset($param['batch']) ? $param['batch'] : '';  // 出库批次号
            $business_sn_in = isset($param['business_sn_in']) ? $param['business_sn_in'] : '';  // 入库业务单号
            $business_sn = isset($param['business_sn']) ? $param['business_sn'] : '';  // 出库业务单号
            $sku = isset($param['sku']) ? $param['sku'] : '';  // sku
            $create_time = isset($param['create_time']) ? $param['create_time'] : '';  // 入库时间
            $offset = ($pageNumber - 1) * $pageData;
            $limit = " LIMIT $offset,$pageData";


            if (!empty($id)) {
                $id = trim($id);
                $where .= " AND SWC.id = $id";
            }
            if (!empty($base_warehouse_id)) {
                $base_warehouse_id = trim($base_warehouse_id);
                $where .= " AND SWC.base_warehouse_id = $base_warehouse_id";
            }
            if (!empty($sku)) {
                $sku = trim($sku);
                $where .= " AND SWC.sku = '{$sku}'";
            }
            if (!empty($batch)) {
                $batch = trim($batch);
                $where .= " AND SWC.batch = '{$batch}'";
            }
            if (!empty($business_sn_in)) {
                $business_sn_in = trim($business_sn_in);
                $where .= " AND SWC.business_sn_in = '{$business_sn_in}'";
            }
            if (!empty($business_sn)) {
                $business_sn = trim($business_sn);
                $where .= " AND SWCD.business_sn = '{$business_sn}'";
            }
            if (!empty($create_time)) {
                $date = explode(' - ', $create_time);
                if (count($date) == 2){
                    $where .= " AND SWC.create_time >= '$date[0]'";
                    $where .= " AND SWC.create_time <= '$date[1]'";
                }
            }
            if (!empty($warehouse_shop_permission)) {
                $warehouse_shop_permission = trim($warehouse_shop_permission);
                $where .= " AND SWC.base_warehouse_id in ($warehouse_shop_permission)";
            }else{
                $where .= " AND SWC.base_warehouse_id in ('')";
            }

            $sqlCount = "SELECT COUNT(SWC.id) num FROM shop_warehouse_cost SWC LEFT JOIN shop_warehouse_cost_detail SWCD
            ON SWC.id = SWCD.shop_cost_id {$where}";
            $countArr = ToolModel::queryData($sqlCount);
            if (empty($countArr) || $countArr[0]['num'] == 0) {
                if ($is_download) exitJson(["code" => "500", "msg" => "无有效数据"]);
                return array('total' => 0, 'list' => array());
            }
            $fileds = 'SWC.*,SWCD.business_sn';
            $sql = "SELECT {$fileds} FROM shop_warehouse_cost SWC LEFT JOIN shop_warehouse_cost_detail SWCD
            ON SWC.id = SWCD.shop_cost_id {$where} ORDER BY SWC.id DESC {$limit}";
            if ($is_download) return "SELECT {$fileds} FROM shop_warehouse_cost SWC LEFT JOIN shop_warehouse_cost_detail SWCD
            ON SWC.id = SWCD.shop_cost_id {$where} ORDER BY id DESC ";

            $list = ToolModel::queryData($sql);
            $data = array();
            $data['total'] = intval($countArr[0]['num']);
            $data['list'] = $list;

            return $data;
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 导出数据
     * @param $param
     * @return array
     * @author duanwenjie
     */
    static public function export($param)
    {
        $sql = self::getList($param,true);
        $title = array(
            'base_warehouse_name' => '仓库名称',
            'batch' => '入库批次号',
            'business_sn_in' => '入库业务单号',
            'sku' => 'SKU',
            'sku_name' => 'SKU中文名称',
            'quantity' => '总入库数量',
            'price' => '批次单价',
            'surplus_quantity' => '批次结余数量',
            'create_time' => '入库时间'
        );

        $res = DownService::getData($sql,$title,'',$param);

        return $res;
    }

    /**
     * 获取店仓成本明细
     * @param $param
     * @return false|\PDOStatement|string|\think\Collection
     * @author duanwenjie
     */
    static public function getShopwarehousecostDetail($param)
    {
        $id = $param['id'];
        $res = Db::name('shop_warehouse_cost_detail')->field('*')->where('shop_cost_id','=',$id)->select();

        return $res;
    }

}